﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using HRProject.Code.DTO;
using System.Collections.Generic;

namespace HRProject.Code.DAO
{
    public class AssetPossessionDAO : BaseDAO
    {
        public AssetPossessionDAO()
            : base ()
        {
        }

        public List<BLAssetPossession> search(BLAssetPossession filter)
        {
            List<BLAssetPossession> result = new List<BLAssetPossession>();
            List<BaseCondition> condition = new List<BaseCondition>();
            condition.Add(new BaseCondition("@assetId", filter.Asset.Id,ParameterDirection.Input));
            condition.Add(new BaseCondition("@assetName", filter.Asset.Name,ParameterDirection.Input));
            condition.Add(new BaseCondition("@assetTypeId", filter.Asset.Type.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@userId", filter.UserId, ParameterDirection.Input));
            DataTable data = select("usp_SearchAssetPossessions", condition);
            for (int i = 0; i < data.Rows.Count; i++)
            {
                BLAssetPossession possession = new BLAssetPossession();
                possession.Description = (string)data.Rows[i]["Description"];
                possession.Asset = new BLAsset();
                possession.Asset.Id = (Guid)data.Rows[i]["AssetId"];
                possession.Asset.Name = (string)data.Rows[i]["AssetName"];
                possession.UserId = (Guid)data.Rows[i]["UserId"];
                possession.StartDate = (DateTime)data.Rows[i]["StartDate"];
                possession.EndDate = (DateTime)data.Rows[i]["EndDate"];
                possession.DataSignature = (long)data.Rows[i]["DataSignature"];
                possession.CreateDate = (DateTime)data.Rows[i]["CreateDate"];
                possession.CreateUserId = (Guid)data.Rows[i]["CreateUserId"];
                possession.UpdateDate = (DateTime)data.Rows[i]["UpdateDate"];
                possession.UpdateUserId = (Guid)data.Rows[i]["UpdateUserId"];
                result.Add(possession);
            }
            return result;
        }

        public int insert(BLAssetPossession asset)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@assetId", asset.Asset.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@userId", asset.UserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startDate", asset.StartDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endDate", asset.EndDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", asset.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@createUserId", Membership.GetUser().ProviderUserKey.ToString(),ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature",asset.DataSignature,ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode",errorCode,ParameterDirection.Output));
            int result = update("usp_AddAssetPossession", condition);
            return result;
        }

        public int update(BLAssetPossession asset)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@assetId", asset.Asset.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@userId", asset.UserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@startDate", asset.StartDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@endDate", asset.EndDate, ParameterDirection.Input));
            condition.Add(new BaseCondition("@description", asset.Description, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", Membership.GetUser().ProviderUserKey.ToString(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", asset.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_UpdateAssetPossession", condition);
            return result;
        }

        public int delete(BLAssetPossession asset)
        {
            List<BaseCondition> condition = new List<BaseCondition>();
            int errorCode = 0;
            condition.Add(new BaseCondition("@assetId", asset.Asset.Id, ParameterDirection.Input));
            condition.Add(new BaseCondition("@userId", asset.UserId, ParameterDirection.Input));
            condition.Add(new BaseCondition("@updateUserId", Membership.GetUser().ProviderUserKey.ToString(), ParameterDirection.Input));
            condition.Add(new BaseCondition("@dataSignature", asset.DataSignature, ParameterDirection.Input));
            condition.Add(new BaseCondition("@errorCode", errorCode, ParameterDirection.Output));
            int result = update("usp_DeleteAssetPossession", condition);
            return result;
        }
    }
}
